package com.only.common.util;

import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * @author only
 * @date 2023/10/17
 * @description
 */
public class ListAndStringConverter {
    private static final String SplitKey = ",";

    public static String listToString(List<String> list) {
        return listToString(list, SplitKey);
    }

    public static String listToString(List<String> list, String splitKey) {
        StringJoiner sj = new StringJoiner(splitKey);
        list.forEach(sj::add);
        return sj.toString();
    }

    public static List<String> stringParsingToList(String str) {
        if (StrUtil.isBlank(str)) {
            return new ArrayList<>();
        }

        return Arrays.stream(str.split(SplitKey)).collect(Collectors.toList());
    }

    public static List<String> stringParsingToList(String str, String splitKey) {
        if (StrUtil.isBlank(str)) {
            return new ArrayList<>();
        }

        return Arrays.stream(str.split(splitKey)).collect(Collectors.toList());
    }
}
